package com.luojiaoj.user

import com.luojiaoj.User
import com.luojiaoj.UserRole
import com.luojiaoj.Role

class UserService {

    public User createUser(User userParams, Role role) {
        def user = User.findByUsername(userParams.username);

        if (!user) {
            user = new User(userParams).save(failInError: true);
        }

        def adminUserRole = UserRole.findByUserAndRole(user, role);

        if (!adminUserRole) {
            UserRole.create(user, role, true);
        }

        return user
    }
}
